home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
ada
/
c01lab4.zip
/
LRMRDR
/
LRM_CODE.ZIP
/
DAF.A
< prev
next >
Wrap
Text File
|
1992-05-29
|
9KB
|
269 lines
-- COPYRIGHT NOTICE
-- Ada LRM Reader - Interactive Presentation of the Ada LRM
-- Copyright (C) 1992 Richard Conn
--
-- This program is free software; you can redistribute it
-- and/or modify it under the terms of the GNU General Public
-- License Version 1 as published by the Free Software
-- Foundation.
--
-- This program is distributed in the hope that it will be
-- useful, but WITHOUT ANY WARRANTY; without even the implied
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-- PURPOSE. See the GNU General Public License for more
-- details. You should have received a copy of the GNU General
-- Public License along with this program; if not, write to the
-- Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
-- 02139, USA. See the ABOUT screens for further information,
-- including information on how to contact the author.
with SYSDEP;
package DAF_Handler is
-- Handler for Direct Access Files (DAFs)
-- Types of LINEs (records) in DAFs
type LINE_TYPE is (NORMAL, SECTION, UNUSED);
-- The LINE is the record of a DAF
type LINE is record
Str : STRING (1..SYSDEP.Screen_String_Length);
Str_Last : NATURAL := 0; -- index of last char in Str
Kind : LINE_TYPE := NORMAL;
end record;
subtype LINE_NUMBER is NATURAL range 1..NATURAL'LAST;
subtype DAF_ID is NATURAL range 0..SYSDEP.Citation_Stack_Depth;
-- Exceptions
DAF_CREATION_ERROR : exception;
FILE_NOT_FOUND : exception;
NO_DAF_OPEN : exception;
READ_ERROR : exception;
WRITE_ERROR : exception;
STACK_OVERFLOW : exception;
UNEXPECTED_ERROR : exception;
-- Subprograms to create a DAF file
procedure Create (File_Name : in STRING);
-- Create a DAF file
procedure Write (Item : in STRING);
-- Write a string to a DAF file
procedure Close_Create;
-- Close a DAF file
-- Subprograms to read DAF files
function Open (File_Name : in STRING) return DAF_ID;
-- Open an existing DAF file
function Is_Open (ID : in DAF_ID) return BOOLEAN;
-- Determine if the DAF file is currently open
function Is_End_of_File (ID : in DAF_ID) return BOOLEAN;
-- Determine if the end of a DAF file has been reached
function Read (ID : in DAF_ID;
Lnum : in LINE_NUMBER) return LINE;
-- Read a specified line from a DAF file
function Read_Next (ID : in DAF_ID) return LINE;
-- Read the next line from a DAF file
procedure Close (ID : in DAF_ID);
-- Close a DAF file
end DAF_Handler;
with Direct_IO;
package body DAF_Handler is
-- I/O Subsystem for DAF Files
package DAF_IO is new Direct_IO (LINE);
-- Flag to mark a file ID as available or not available
type USE_FLAG is (UNAVAILABLE, AVAILABLE);
-- Class of Stack of file IDs
type FILE_ID_STACK is array (1..DAF_ID'LAST) of DAF_IO.FILE_TYPE;
-- Class of Stack of use flags
type FILE_USE_STACK is array (1..DAF_ID'LAST) of USE_FLAG;
-- Actual stacks
Stack : FILE_ID_STACK;
Stack_Use : FILE_USE_STACK := (others => AVAILABLE);
-- File_Type used for output DAF
Create_File_ID : DAF_IO.FILE_TYPE;
-- Conversion function between STRING and LINE
-------------------------------------------------------------------------
function Convert (Item : in STRING;
Kind : in LINE_TYPE := NORMAL) return LINE is
-- Return the indicated Item as a LINE
Result : LINE;
begin -- Convert
Result.Str_Last := Item'LENGTH;
Result.Str(1..Result.Str_Last) := Item;
Result.Kind := Kind;
return Result;
end Convert;
-- Subprograms to create a DAF file
-------------------------------------------------------------------------
procedure Create (File_Name : in STRING) is
-- Create a DAF file
begin -- Create
DAF_IO.Create (Create_File_ID, DAF_IO.OUT_FILE, File_Name);
exception -- Create
when others => raise DAF_CREATION_ERROR;
end Create;
-------------------------------------------------------------------------
procedure Write (Item : in STRING) is
-- Write a string to a DAF file
procedure Write_Line (Line : in STRING;
Line_Kind : LINE_TYPE) is
-- Write a line, creating a continuation line if necessary
begin -- Write_Line
if Line'LENGTH > SYSDEP.Screen_Width then
DAF_IO.Write (Create_File_ID,
Convert (" " &
Line(Line'FIRST ..
Line'FIRST-1+SYSDEP.Screen_Width),
Line_Kind));
DAF_IO.Write (Create_File_ID,
Convert ("| " &
Line(Line'FIRST+SYSDEP.Screen_Width..
Line'LAST),
Line_Kind));
else
DAF_IO.Write (Create_File_ID,
Convert (" " & Line, Line_Kind));
end if;
end Write_Line;
begin -- Write
if Item'LENGTH > 2 and then Item(Item'FIRST..Item'FIRST+1) = "> " then
Write_Line (Item(Item'FIRST+2..Item'LAST), DAF_Handler.SECTION);
-- section line
else
Write_Line (Item, DAF_Handler.NORMAL);
-- normal line
end if;
exception -- Write
when others => raise WRITE_ERROR;
end Write;
-------------------------------------------------------------------------
procedure Close_Create is
-- Close a DAF file
begin -- Close_Create
DAF_IO.Close (Create_File_ID);
exception -- Close_Create
when others => raise UNEXPECTED_ERROR;
end Close_Create;
-- Subprograms to read DAF files
-------------------------------------------------------------------------
function Open (File_Name : in STRING) return DAF_ID is
--Open an existing DAF file
Available_ID : DAF_ID := 0;
begin -- Open
-- Locate an available DAF_ID
for I in Stack_Use'RANGE loop
if Stack_Use(I) = AVAILABLE then
Available_ID := I;
Stack_Use(I) := UNAVAILABLE;
exit;
end if;
end loop;
-- Abort if no DAF_ID is available
if Available_ID = 0 then
raise STACK_OVERFLOW;
end if;
-- Open file
DAF_IO.Open (Stack(Available_ID), DAF_IO.IN_FILE, File_Name);
return Available_ID;
exception -- Open
when STACK_OVERFLOW => raise;
when others => raise FILE_NOT_FOUND;
end Open;
-------------------------------------------------------------------------
function Is_Open (ID : in DAF_ID) return BOOLEAN is
-- Determine if the DAF file is currently open
begin -- Is_Open
if ID = 0 then
raise NO_DAF_OPEN;
end if;
return DAF_IO.IS_OPEN (Stack(ID));
exception -- Is_Open
when NO_DAF_OPEN => raise;
when others => raise UNEXPECTED_ERROR;
end Is_Open;
-------------------------------------------------------------------------
function Is_End_of_File (ID : in DAF_ID) return BOOLEAN is
-- Determine if the end of a DAF file has been reached
begin -- Is_End_of_File
if ID = 0 then
raise NO_DAF_OPEN;
end if;
return DAF_IO.END_OF_FILE (Stack(ID));
exception -- Is_End_of_File
when NO_DAF_OPEN => raise;
when others => raise UNEXPECTED_ERROR;
end Is_End_of_File;
-------------------------------------------------------------------------
function Read (ID : in DAF_ID;
Lnum : in LINE_NUMBER) return LINE is
-- Read a specified line from a DAF file
Outline : LINE;
begin -- Read
if ID = 0 then
raise NO_DAF_OPEN;
end if;
DAF_IO.Read (Stack(ID), Outline, DAF_IO.POSITIVE_COUNT(Lnum));
return Outline;
exception -- Read
when NO_DAF_OPEN => raise;
when others => raise READ_ERROR;
end Read;
-------------------------------------------------------------------------
function Read_Next (ID : in DAF_ID) return LINE is
-- Read the next line from a DAF file
Outline : LINE;
begin -- Read_Next
if ID = 0 then